<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    //数组截取
    let arr = [1, 2, 3, 4]
    let res = arr.slice(1, 3)
    console.log(res);
    //数组排序
    let arr2 = [2, 5, 3, 6, 4, 8, 7, 9, 0]
    let newArr2 = arr2.sort()
    console.log(arr2);
    //冒泡排序
    for (let i = 0; i < arr2.length - 1; i++) {//控制比较轮廓
        for (let j = 0; j < arr2.length - 1 - i; j++) {
            if (arr2[j] > arr2[j + 1]) {
                let tempt = arr2[j]
                arr2[j] = arr2[j + 1]
                arr2[j + 1] = tempt
            }

        }

    }
    console.log(arr2);
    //sort()函数的用法
    let arr3 = [4, 5, 33, 9, 1, 8, 3, 2, 10]
    console.log(arr3.sort());
    let newArr3 = arr3.sort(function (a, b) {
        return a - b//升序  b-a降序
    })
    console.log(newArr3);
    //对象排序
    let obj = {
        name: '张三',
        age: '18',
        sex: '男',
        job: '学生'
    }
    console.log(obj.name);//调用对象属性
    let stNum = [
        {
            nam: "张三",
            score: 78,
            age: 64


        },
        {
            nam: "李四",
            score: 74,
            age: 90
        },
        {
            nam: "王五",
            score: 60,
            age: 40
        }
    ]
    stNum.sort(function (a, b) {
        return b.score - a.age//按年龄排序
    })
    console.log(stNum);
    //数组倒序,
    let arr4 = [1, 4, 8, 3, 5, 2]
    console.log(arr4);
    //arr4.reverse
    arr4.sort().reverse()
    console.log(arr4);
    //map()方法  不会改变原数组，返回值为数组

    let arr5 = [1, 3, 4, 7, 2, 6,]
    // for(let i=0;j<arr5.length;i++){
    //     arr5[i]*2
    // }
    let newArr5 = arr5.map(function (item, index, arr) {
        return item * 2
    })
    console.log(newArr5);
    //数组遍历
    let arr6 = [1, 2, 3, 4, 5, 6]
    for (let i = 0; i < arr6.length; i++) { console.log((arr6[i])); }
    // for in 循环
    for (index in arr6) {
        console.log(index, '______' + arr6[index]);
    }
    //for of 循环 遍历数组的每一项
    for (item of arr6) {
        console.log(item);
    }
    //forEach 遍历数组
    let sum = 0
    arr6.forEach(function (item, index, arr) {
        // console.log(item, index, arr, '---------');
        if (item % 2 == 0) {
            sum += item * 2
        }
    })
    console.log(sum);
    //filter过滤筛选
    let newarr6back = arr6.filter(function (item, index, arr) {
        return item > 2 && item < 5
    })
    console.log(newarr6back);

    let students = [
        { name: '张三', sex: '男' },
        { name: '王伟', sex: '女' },
        { name: '郝帅', sex: '男' },
        { name: '真帅', sex: '女' },
    ]
    let newst = students.filter(function (item) {
        return item.sex == '男'
    }
    )
    console.log(newst);

    //查询元素
    let arr7 = [1, 2, 3, '我', 'a']
    console.log(arr7.indexOf('a'));
    console.log(arr7.lastIndexOf('4'));
    console.log(arr7.includes('3'));
    console.log(arr7.find(function (item) {
        return item == '2'
    }));
    console.log(arr7.findIndex(function (item) {
        return item == 'a'
    }));
    //数组转字符串
    let arr7str = arr7.join('7')
    console.log(arr7str);
    //
    let str = 'a,b,k,g,z.3,5,8,2'
    let newStr = str.split(',').sort(function (a, b) {
        return a - b
    }).join('*')
    console.log(newStr)
    //数组累加方法
    let arr8 = [22, 44, 66, 45, 77, 88, 98]
    let sumArr8 = arr8.reduce(function (pre, cur, index, arr) {
        return pre += cur
    }, 0)
    console.log(sumArr8);
    let commodity = [
        { name: '牛奶', prc: 3 },
        { name: '可乐', prc: 6 },
        { name: '泡面', prc: 5 },
        { name: '牙膏', prc: 10 },
    ]
    let prcsum = commodity.reduce(function (pre, cur) {
        return pre += cur.prc
    })
    console.log(`商品总价为：${prcsum}元`);
    //数组去重

    let arr9 = [1, 1, 1, 3, 3, 5, 76, 88, 4, 4, 3, 2, 2]
    let arr9back = arr9.reduce((pre, cur)=>{
        if (!pre.includes(cur)) {//如果该元素再新数组中不存在
            return pre.concat(cur) //将该元素添加到新数组中
        } else {
            return pre
        }
    }, [])
    console.log(arr9back);// [1, 2, 3, 4]

</script>